import { defineConfig, loadEnv } from 'vite'
const { resolve } = require('path')
import createVitePlugins from './vite/plugins'
// https://vitejs.dev/config/
export default ({ mode, command }) => {
  const env = loadEnv(mode, process.cwd())
  const { VITE_APP_BASE } = env
  return defineConfig({
    // base: '/crm',
    externals: {
      BMap: 'BMap'
    },
    resolve: {
      dedupe: [
        'vue'
      ],
      alias: {
        '~': resolve(__dirname, './'),
        "@": resolve(__dirname, "./src"),
        "components": resolve(__dirname, "./src/components"),
        "styles": resolve(__dirname, "./src/styles"),
        "utils": resolve(__dirname, "./src/utils"),
      },
      extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
    },
    css: {
      preprocessorOptions: {
        scss: {
          // 两种方式都可以
          additionalData: '@import "/src/styles/xr-theme.scss";'
          // additionalData: '@use "/src/assets/scss/global.scss" as *;'
        }
      }
    },
    server: {
      // host设置为true才可以使用network的形式，以ip访问项目
      host: true,
      // 本地编译后访问的端口号
      port: 8082,
      // 编译后是否自动打开浏览器访问
      open: false,
      hmr: {
        overlay: false
      },
      // 跨域设置允许
      cors: false,
      // 如果端口已占用直接退出
      strictPort: false,
    },

    plugins: createVitePlugins(env, command === 'build'),
    build: {
      outDir: "crm",
      rollupOptions: {
        external: [
          // "element-plus"
        ]
      }
    }
  })
}
